export const state = () => ({
  user : null ,
})

export const mutations = {
  login(state,user) {
    let str = JSON.stringify(user)
    state.user = user ;
    localStorage.setItem('user', str)
  },
  autoLogin(state,user){
    state.user = user ;
  }
}

export const getters = {
  getUser: state => {
    if(state.user == null) {
      let user = localStorage.getItem('user') ;
      if(!user) {
        return false ;
      }
      let userObj = JSON.parse(user);
      this.$store.commit('autoLogin', userObj)
      return userObj ;
    }

    return state.user ;
  }
}

